package com.os.opencv.java.chapter4;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class Lut2 {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //读取图像文件并在屏幕上显示
        Mat src = Imgcodecs.imread("/Users/matt/MATT-MAC/matt_workspace/OS-OpenCV_workspace/eyeTest.jpeg");
        Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);

        //在屏幕上显示图像灰度图
        HighGui.imshow("src", src);
        HighGui.waitKey(0);

        //创建查找表
        Mat lut = new Mat(new Size(256, 1), CvType.CV_8U);

        //设置查找表，将灰度值分为8个层级，总体亮度减半
        byte[] i = new byte[256];
        for(int n=0; n<256; n++){
            Byte b = (byte) (n/32);
            i[n] = (byte) (b*32);
        }
        lut.put(0,0,i);

        //用查找表里的值填充输出矩阵
        Mat dst = new Mat();
        Core.LUT(src, lut, dst);

        //在控制台处处替换后的图像
        HighGui.imshow("after lut", dst);
        HighGui.waitKey(0);

        //对替换后的图像进行自适应二值化处理并在屏幕上显示
        int adaptiveMethod = Imgproc.ADAPTIVE_THRESH_MEAN_C;
        int thresholdType = Imgproc.THRESH_BINARY;
        Imgproc.adaptiveThreshold(dst, dst, 255, adaptiveMethod, thresholdType, 7, 8);
        HighGui.imshow("afterLut adaptive", dst);
        HighGui.waitKey(0);

        //对原图像进行自适应二值化处理并在屏幕上显示
        Imgproc.adaptiveThreshold(src, dst, 255, adaptiveMethod, thresholdType, 7, 8);
        HighGui.imshow("src, adaptive", dst);
        HighGui.waitKey(0);

        System.exit(0);

    }
}
